package com.baselib.libnetwork.net.utils;//package com.ganesha.sdk.net;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.List;


public class Convert {
    public static <T> List<T> getArray(String json, Class<T> cls) {
        List<T> list = null;
        try {
            list = JSON.parseArray(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static <T> T getBean(String json, Class<T> cls) {
        T t = null;
        try {
            t = JSON.parseObject(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T getBean(String json, TypeReference<T> type) {
        T t = null;
        try {
            t = JSON.parseObject(json, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static JSONObject getObject(String json) {
        JSONObject obj = null;
        try {
            obj = JSON.parseObject(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static JSONArray getArray(String json) {
        JSONArray obj = null;
        try {
            obj = JSON.parseArray(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static int getIntValue(JSONObject obj, String key) {
        int result = 0;
        if (obj == null) {
            return result;
        }
        try {
            result = obj.getIntValue(key);
        } catch (Exception e) {
        }
        return result;
    }

    public static int getIntValue(JSONObject obj, String key, int def) {

        int result = def;
        if (obj == null) {
            return result;
        }
        try {
            result = obj.getIntValue(key);
        } catch (Exception e) {
        }
        return result;
    }

}
